package com.gov.ec.rsv.servicio;

import com.gov.ec.rsv.dao.CrudDAO;
import com.gov.ec.rsv.interfaces.IFacturaServicio;
import com.gov.ec.rsv.modelo.Cabreservacion;
import com.gov.ec.rsv.modelo.Detreservacion;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import org.apache.log4j.Logger;

/**
 * The Class OrdenProduccionServicio.
 *
 * @author IGM
 */
@Stateless
@LocalBean
public class FacturaServicio extends CrudDAO implements IFacturaServicio {

    /** La variable localLogger. */
    public static Logger localLogger = Logger.getLogger(Cabreservacion.class);

   
    @Override
    public List<Object>busFecPlanOp(final Date fechaInicio, final Date fechaFin) {
        try {
            
        	Query query = em.createQuery("select d.fechaFin, count(d.fechaFin), d.servicio.descripcion," +
        			" d.servicio.instalacione.capacidad -count(d.fechaFin) as libres from Detreservacion d " +
        			"where d.fechaFin >= ?1 and  d.fechaFin <= ?3 " +
        			"group by d.fechaFin, d.servicio.descripcion " +
        			"order by d.fechaFin");
            query.setParameter(1, fechaInicio, TemporalType.TIMESTAMP);
            query.setParameter(3, fechaFin, TemporalType.TIMESTAMP);
            return query.getResultList();
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
            return new ArrayList<Object>();
        }
    }
    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#buscarSuc(java.lang.Long)
	 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cabreservacion> buscarSuc(final Long numSuc) {
        List<Cabreservacion> lista = new ArrayList<Cabreservacion>();
        try {
            Query query = em.createQuery("select o from Cabreservacion as o "
                    + "where o.CabreservacionPK.idSucursal = ?1 "
                    + "order by o.fecha desc");
            query.setParameter(1, numSuc);
            lista.addAll(query.getResultList());
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
        }
        return lista;
    }

    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#buscarFac(java.util.Date, java.util.Date, short)
	 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cabreservacion> buscarFac(final Date fechaInicio, final Date fechaFin, final short sucursal) {
        try {
            String aux = " o.CabreservacionPK.idSucursal =?3 ";
            Query query = em.createQuery("select o from Cabreservacion as o "
                    + "where o.fecha between ?1 and ?2 "
                    + ((-1 != sucursal) ? aux : "")
                    + "order by o.fecha desc");
            query.setParameter(1, fechaInicio, TemporalType.TIMESTAMP);
            query.setParameter(2, fechaFin, TemporalType.TIMESTAMP);
            if (-1 != sucursal) {
                query.setParameter(3, sucursal);
            }
            return query.getResultList();
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
            return new ArrayList<Cabreservacion>();
        }
    }

   /* (non-Javadoc)
 * @see com.gov.ec.rsv.servicio.IFacturaServicio#buscaFactura(java.lang.Long)
 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cabreservacion> buscaFactura(final Long numFac) {
        List<Cabreservacion> lista = new ArrayList<Cabreservacion>();
        try {
            Query query = super.em.createQuery("select o from Cabreservacion as o "
                    + "where o.CabreservacionPK.idFactura = ?1 ");
            query.setParameter(1, numFac);
            lista.addAll(query.getResultList());
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
        }
        return lista;

    }
    
      
    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#buscarFact(java.util.Date, java.util.Date, int, int)
	 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cabreservacion> buscarFact(final Date fechaInicio, final Date fechaFin,
            final int maxResults, final int firstResult) {
        try {
            Query query = em.createQuery("select o from Cabreservacion as o "
                    + "where o.fecha between ?1 and ?3 ");
            query.setParameter(1, fechaInicio, TemporalType.TIMESTAMP);
            query.setParameter(3, fechaFin, TemporalType.TIMESTAMP);
            query.setMaxResults(maxResults);
            query.setFirstResult(firstResult);
            return query.getResultList();
            
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
            return new ArrayList<Cabreservacion>();
        }
        
    }

    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#conteo(java.util.Date, java.util.Date)
	 */
    @Override
	public Long conteo(final Date fechaInicio, final Date fechaFin) {
        try {

            Query query = em.createQuery("select count(o) from Cabreservacion as o "
                    + "where o.fecha between ?1 and ?3 ");
            query.setParameter(1, fechaInicio, TemporalType.TIMESTAMP);
            query.setParameter(3, fechaFin, TemporalType.TIMESTAMP);
            return Long.valueOf(query.getSingleResult().toString());
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
            return 0L;
        }
    }

    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#contarFac(java.util.Date, java.util.Date, short)
	 */
    @Override
	public Long contarFac(Date fechaInicio, Date fechaFin, final short numSucursal) {
        try {
            String aux = " o.CabreservacionPK.idSucursal =?3 ";
            Query query = em.createQuery("select count(o.CabreservacionPK.idFactura) from Cabreservacion as o "
                    + "where o.fecha between ?1 and ?2 "
                    + ((-1 != numSucursal) ? aux : "")
                    + "order by o.fecha desc");
            query.setParameter(1, fechaInicio, TemporalType.TIMESTAMP);
            query.setParameter(2, fechaFin, TemporalType.TIMESTAMP);
            if (-1 != numSucursal) {
                query.setParameter(3, numSucursal);
            }
            return (Long) query.getSingleResult();
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
            return 0L;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IFacturaServicio#buscaProforma(java.lang.Long)
	 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cabreservacion> buscaProforma(final Long numFac) {
        List<Cabreservacion> lista = new ArrayList<Cabreservacion>();
        try {
            Query query = super.em.createQuery("select o from Cabreservacion as o "
                    + "where o.CabreservacionPK.idFactura = ?1 ");
            query.setParameter(1, numFac);
            lista.addAll(query.getResultList());
        } catch (Exception e) {
            localLogger.error(e.getMessage(), e);
        }
        return lista;

    }
    
    
    
}
